<template>
	<view class="u-p-l-26 u-p-r-26 u-p-t-26 bg-white">
		<u-form :model="form" ref="formEl" labelWidth="130" input-align="right">
			<u-form-item label="姓名" prop="fullName">
				<u-input v-model="form.fullName"></u-input>
			</u-form-item>
			<u-form-item label="昵称" v-if="edit" prop="nickName">
				<u-input v-model="form.nickName"></u-input>
			</u-form-item>
			<u-form-item label="手机号" prop="phone">
				<u-input v-model="form.phone" type="number"></u-input>
			</u-form-item>
			<u-form-item label="性别" prop="phone">
				<u-radio-group v-model="form.sex" active-color="#ffc300">
					<u-radio :name="1">男</u-radio>
					<u-radio :name="2">女</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>
	</view>

	<view class="u-p-l-32 u-p-r-42 u-p-t-42">
		<u-button type="warning" shape="circle" @click="editFun">确定</u-button>
	</view>
</template>

<script setup>
	import {
		onReady,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref
	} from "vue";
	import shopApi from '@/api/shop'
	onLoad((e) => {
		if (e.id) {
			shopApi.getSalesmanInfo({
				id: e.id
			}).then(res => {
				let obj = res.data
				const includedKeys = ['fullName', 'nickName', 'phone', 'saleType', 'sex','casUserId','userId',"saleId"];
				const result = copyObjectWithInclusion(obj, includedKeys);
				form.value = result
			})
		}

		if(e.update){
			edit.value = true
		}
	})

	const formEl = ref(null)
	const edit = ref(false)

	const form = ref({
		fullName: '',
		nickName: '',
		phone: '',
		saleType: 1,
		sex: 1
	})


	function copyObjectWithInclusion(sourceObject, includedKeys) {
		const targetObject = {};
		for (const key of includedKeys) {
			if (sourceObject.hasOwnProperty(key)) {
				targetObject[key] = sourceObject[key];
			}
		}
		return targetObject;
	}

	function editFun() {
		if (edit.value) {
			shopApi.editSalesman(form.value).then(res => {
				if(res.status==0){
					uni.showToast({
						title: '操作成功',
						icon: "none"
					})
				}else{
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
				}
			})

		} else {
			shopApi.addSalesman(form.value).then(res => {
				if(res.status==0){
					uni.showToast({
						title: '操作成功',
						icon: "none"
					})
				}else{
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
				}
			})
		}

	}
</script>

<style>
</style>
